<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { getOverviewApi } from "@/api/report";
import { ElMessage } from "element-plus";
import {
  Connection,
  Opportunity,
  Reading,
  Calendar,
  User,
  Document,
  House,
} from "@element-plus/icons-vue";

const router = useRouter();

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0,
});

// 获取概览数据
const getOverview = async () => {
  try {
    const result = await getOverviewApi();
    if (result.code === 1) {
      overviewData.value = result.data;
    } else {
      ElMessage.error(result.msg || "获取概览数据失败");
    }
  } catch (error) {
    ElMessage.error("网络请求失败");
  }
};

// 快捷操作跳转
const handleQuickAction = (route) => {
  router.push(route);
};

// 钩子函数
onMounted(() => {
  getOverview();
});
</script>

<template>
  <div class="home">
    <!-- 欢迎标题 -->
    <div class="welcome-section">
      <h1 class="welcome-title">
        <el-icon class="welcome-icon"><House /></el-icon>
        欢迎您, 进入轻客管家系统
      </h1>
    </div>

    <!-- 概览卡片区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <div class="card-header">
          <el-icon class="card-icon"><Connection /></el-icon>
          <span class="card-title">线索概览</span>
        </div>
        <div class="card-content">
          <div class="metric-grid">
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueTotal }}</div>
              <div class="metric-label">总线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueWaitAllot }}</div>
              <div class="metric-label">待分配线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueWaitFollow }}</div>
              <div class="metric-label">待跟进线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueFollowing }}</div>
              <div class="metric-label">跟进中线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueFalse }}</div>
              <div class="metric-label">伪线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">
                {{ overviewData.clueConvertBusiness }}
              </div>
              <div class="metric-label">转商机线索数</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card">
        <div class="card-header">
          <el-icon class="card-icon"><Opportunity /></el-icon>
          <span class="card-title">商机概览</span>
        </div>
        <div class="card-content">
          <div class="metric-grid">
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessTotal }}</div>
              <div class="metric-label">总商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">
                {{ overviewData.businessWaitAllot }}
              </div>
              <div class="metric-label">待分配商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">
                {{ overviewData.businessWaitFollow }}
              </div>
              <div class="metric-label">待跟进商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">
                {{ overviewData.businessFollowing }}
              </div>
              <div class="metric-label">跟进中商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessFalse }}</div>
              <div class="metric-label">回收商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">
                {{ overviewData.businessConvertCustomer }}
              </div>
              <div class="metric-label">转客户商机数</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <div class="quick-actions-grid">
        <!-- 课程管理 -->
        <div
          class="quick-action-card"
          @click="handleQuickAction('/resource/course')"
        >
          <el-icon class="action-icon"><Reading /></el-icon>
          <div class="action-title">课程管理</div>
          <div class="action-subtitle">课程信息维护</div>
        </div>

        <!-- 活动管理 -->
        <div
          class="quick-action-card"
          @click="handleQuickAction('/resource/activity')"
        >
          <el-icon class="action-icon"><Calendar /></el-icon>
          <div class="action-title">活动管理</div>
          <div class="action-subtitle">营销活动管理</div>
        </div>

        <!-- 线索管理 -->
        <div class="quick-action-card" @click="handleQuickAction('/clue/list')">
          <el-icon class="action-icon"><Connection /></el-icon>
          <div class="action-title">线索管理</div>
          <div class="action-subtitle">潜在客户追踪</div>
        </div>

        <!-- 商机管理 -->
        <div
          class="quick-action-card"
          @click="handleQuickAction('/business/list')"
        >
          <el-icon class="action-icon"><Opportunity /></el-icon>
          <div class="action-title">商机管理</div>
          <div class="action-subtitle">销售机会管理</div>
        </div>

        <!-- 客户管理 -->
        <div class="quick-action-card" @click="handleQuickAction('/customer')">
          <el-icon class="action-icon"><User /></el-icon>
          <div class="action-title">客户管理</div>
          <div class="action-subtitle">客户信息维护</div>
        </div>

        <!-- 日志统计 -->
        <div
          class="quick-action-card"
          @click="handleQuickAction('/system/log')"
        >
          <el-icon class="action-icon"><Document /></el-icon>
          <div class="action-title">日志统计</div>
          <div class="action-subtitle">系统操作日志</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100%;
}

/* 欢迎标题区域 */
.welcome-section {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-title {
  font-size: 32px;
  color: #409eff;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.welcome-icon {
  font-size: 36px;
}

/* 概览卡片区域 */
.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-card {
  flex: 1;
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.card-header {
  background: linear-gradient(135deg, #409eff 0%, #36cfc9 100%);
  color: #fff;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.card-icon {
  font-size: 24px;
}

.card-title {
  font-size: 20px;
  font-weight: 600;
}

.card-content {
  padding: 20px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.metric-item {
  text-align: center;
  padding: 15px;
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.metric-item:hover {
  background: #f0f9ff;
  border-color: #409eff;
  transform: translateY(-2px);
}

.metric-value {
  font-size: 28px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 8px;
}

.metric-label {
  font-size: 12px;
  color: #606266;
  line-height: 1.4;
}

/* 快捷操作区域 */
.quick-actions-section {
  margin-bottom: 20px;
}

.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.quick-action-card {
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 25px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.quick-action-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px 0 rgba(0, 0, 0, 0.15);
  border-color: #409eff;
}

.action-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 15px;
}

.action-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
}

.action-subtitle {
  font-size: 14px;
  color: #909399;
  line-height: 1.4;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
  }

  .quick-actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .welcome-title {
    font-size: 24px;
    flex-direction: column;
    gap: 10px;
  }

  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .quick-actions-grid {
    grid-template-columns: 1fr;
  }

  .home {
    padding: 15px;
  }
}
</style>
